<template>
	<view class="content">
		<view class="padding margin_top">
			<view class="flexbetween">
				<view class="word_baiba hei put padding_bottom">收货人</view>
				<input type="text" placeholder="请输入收货人姓名" class="input line padding_bottom word_rb" v-model="name">
			</view>
			<view class="flexbetween margin_top">
				<view class="word_baiba hei put padding_bottom">手机号码</view>
				<input type="text" placeholder="请输入收货人手机号" class="input padding_bottom line word_rb"v-model="mobile">
			</view>
			<view class="flexbetween margin_top">
				<view class="word_baiba hei put padding_bottom">选择地址</view>
				<uni-data-picker  @change="bindPickerChange" :localdata="items" placeholder="请选择住址" popup-title="请选择地区" v-model="area" class="input padding_bottom line word_rb">
					<text class="inputs1" v-if="!area.length" style="color: #999999;">选择省、市、县</text>
					<text class="inputs1" v-else>{{ area[0] }}/{{ area[1] }}/{{ area[2] }}</text>
				</uni-data-picker>
				<view class="greey_icon"></view>
			</view>
			<view class="flexbetween margin_top">
				<view class="word_baiba hei put padding_bottom">详细地址</view>
				<input type="text" placeholder="街道、楼牌号等" class="input padding_bottom line" v-model="addressdetail">
			</view>
			<view class="flexbetween margin_top2">
				<view class="word_baiba hei">设为默认地址</view>
				<view class="">
					<u-switch space="2" v-model="value11" activeColor="#d48b37" inactiveColor="rgb(230, 230, 230)">
					</u-switch>
				</view>
			</view>
		</view>
		<view class="footer_di" @click="tzxzshdz">
			<view class="footer_btn">保存</view>
		</view>
	</view>
</template>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "../static/city/city.js"
	import uniDataPicker from '@/uni_modules/uni-data-picker/components/uni-data-picker/keypress.js';
	export default {
		data() {
			return {
				value11: false,
				area: "",
				listid: "",
				status: "",
				addressdetail: "", //详细地址
				address: {
					province: "", //省
					city: "", //市
					county: "", //区
				},
				name: "", //姓名
				mobile: "", //收货人电话
				data: {},
				value11: false,
				items: citys.data,
			}
		},
		onLoad(options) {
			this.listid = options.id;
			if (this.listid) {
				uni.setNavigationBarTitle({
					title:"编辑收货地址"
				})
			} else {
				uni.setNavigationBarTitle({
					title:"新增收货地址"
				})
			}
			this.dizhixq()
		},
		onShow() {
		},
		methods: {
			isMobile(value) {
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},
			// 修改地址接口
			xiugdz() {
				let _this = this;
				httpRequest.request('/api/user/editAddress', 'GET', {
					// 传参
					id: _this.listid,
					name: _this.name,
					mobile: _this.mobile,
					address_detail: _this.addressdetail,
					status: _this.value11 ? 1 : 0,
					province_id: this.address.province,
					city_id: _this.address.city,
					area_id: _this.address.county
				}, false, false, true).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			// 地址详情接口
			dizhixq() {
				let _this = this;
				httpRequest.request('/api/user/addressDetail', 'GET', {
					// 传参
					id: _this.listid
				}, false, false, true).then(res => {
					if (res.code == 1) {
						this.area = res.data.cityinfo.split("/")
						this.address.province = res.data.province_id
						this.address.city = res.data.city_id
						this.address.county = res.data.area_id
						this.name = res.data.name
						this.mobile = res.data.mobile
						this.addressdetail = res.data.address_detail
						if (res.data.status == 1) { //判断是否默认按钮
							this.value11 = true
						} else {
							this.value11 = false
						}
					}
				})
			},
			tzxzshdz() {
				uni.showLoading({
					title: '请稍候...',
					mask: true
				})
				let _this = this;
				let mobile = _this.mobile;
				if (_this.name == '') {
					uni.hideLoading()
					httpRequest.toast('请输入名字');
					return false;
				}
				if (_this.mobile == "") {
					uni.hideLoading()
					httpRequest.toast('请输入手机号');
					return false;
				}
				if (!this.isMobile(this.mobile)) {
					uni.hideLoading()
					httpRequest.toast("手机号输入有误")
					return false
				}
				if (_this.area == false) {
					uni.hideLoading()
					httpRequest.toast('请选择地址');
					return false;
				}
				if (_this.addressdetail == false) {
					uni.hideLoading()
					httpRequest.toast('请输入详细地址');
					return false;
				}
				if (this.listid) {
					this.xiugdz()
				} else { //添加地址
					httpRequest.request('/api/user/addAddress', 'GET', {
						// 传参
						name: this.name,
						mobile: this.mobile,
						address_detail: this.addressdetail,
						status: this.value11 ? 1 : 0,
						province_id: this.address.province,
						city_id: this.address.city,
						area_id: this.address.county
					}, false, false, true).then(res => {
						uni.hideLoading()
						httpRequest.toast(res.msg)
						if (res.code == 1) {
							setTimeout(() => {
								uni.navigateBack(1)
							}, 1000)
						}
					})
				}
			},
			bindPickerChange(val) {
				let area = val.detail.value
				this.area = [area[0].text, area[1].text, area[2].text];
				this.address.province = area[0].value;
				this.address.city = area[1].value;
				this.address.county = area[2].value;
			},
			submit() {
				let _this = this;
				httpRequest.request('')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.put {
		width: 30%;
	}

	.input {
		width: 70%;
	}

	.fan {
		margin-left: 70rpx;
		margin-bottom: 25rpx;
	}

	.footer_btn {
		font-size: 32rpx;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
		padding: 20rpx 20rpx;
		background-color:#d48b37;
		border-radius: 64rpx;
	}

	.footer_di {
		width: 100%;
		padding: 30rpx 30rpx 30rpx 30rpx;
		background-color: #ffff;
		position: fixed;
		bottom: 0rpx;
		box-sizing: border-box;
	}
</style>